<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="130px" label-position="left" style="margin-top: 300px">
      <el-row>
        <el-col :span="6">
          &nbsp;
        </el-col>
        <el-col :span="24">
          <el-form-item prop="materialCode" class="labelRow">
            <template slot="label">
              <div class="label-icon">
                <el-image :src="require('@/assets/images/cp-icon.png')" class="icon-img"></el-image>
                <span class="label-text">产品码：</span>
              </div>
            </template>
            <el-input v-model="queryParams.materialCode" placeholder="请输入产品码" clearable
                      @keyup.enter.native="handleQuery" size="medium"/>
          </el-form-item>
          <el-form-item prop="materialModeCode" class="labelRow">
            <template slot="label">
              <div class="label-icon">
                <el-image :src="require('@/assets/images/wl-icon.png')" class="icon-img"></el-image>
                <span class="label-text">物料代码：</span>
              </div>
            </template>
            <el-input v-model="queryParams.materialModeCode" placeholder="请输入物料代码" clearable
                      @keyup.enter.native="handleQuery" size="medium"/>
          </el-form-item>
          <el-form-item prop="workOrderNo" class="labelRow">
            <template slot="label">
              <div class="label-icon">
                <el-image :src="require('@/assets/images/gd-icon.png')" class="icon-img"></el-image>
                <span class="label-text">工单号：</span>
              </div>
            </template>
            <el-input v-model="queryParams.workOrderNo" placeholder="请输入工单号" clearable
                      @keyup.enter.native="handleQuery" size="medium"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="searchRow">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery" size="medium">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery" size="medium">重置</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          &nbsp;
        </el-col>
      </el-row>

<!--      <el-form-item label="物料箱码" prop="materialBoxCode">-->
<!--        <el-input v-model="queryParams.materialBoxCode" placeholder="请输入物料箱码" clearable-->
<!--                  @keyup.enter.native="handleQuery"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="托盘码" prop="materialTrayCode">-->
<!--        <el-input v-model="queryParams.materialTrayCode" placeholder="请输入托盘码" clearable-->
<!--                  @keyup.enter.native="handleQuery"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作类型" prop="operateType">-->
<!--        <el-select v-model="queryParams.operateType" placeholder="请选择操作类型" clearable size="small">-->
<!--          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MATERIAL_OPERATE_TYPE)"-->
<!--                     :key="dict.value" :label="dict.label" :value="dict.value"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作人" prop="userId">-->
<!--        <el-select v-model="queryParams.userId" placeholder="请选择操作人" clearable size="small">-->
<!--          <el-option v-for="user in users" :key="user.id" :label="user.nickname"-->
<!--                     :value="user.id"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作时间" prop="createTime">-->
<!--        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                        type="daterange"-->
<!--                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"-->
<!--                        :default-time="['00:00:00', '23:59:59']"/>-->
<!--      </el-form-item>-->

    </el-form>

    <!-- 操作工具栏 -->
<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"-->
<!--                   :loading="exportLoading"-->
<!--                   v-hasPermi="['hs:material-log:export']">导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="90%" v-dialogDrag append-to-body>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="noshadowTable">
        <el-table-column type="index" label="序号" align="center" width="50"/>
        <el-table-column label="产品码" align="center" prop="materialCode" min-width="150"/>
        <el-table-column label="工单号" align="center" prop="workOrderNo" min-width="150">
          <template v-slot="scope">
            <a @click="openMaterialDetail(scope.row.workOrderNo)" style="color:#409eff;cursor:pointer">{{
                scope.row.workOrderNo
              }}</a>
          </template>
        </el-table-column>
        <el-table-column label="物料代码" align="center" prop="materialModeCode" min-width="150"/>
        <el-table-column label="箱码" align="center" prop="materialBoxCode" min-width="150"/>
        <el-table-column label="托盘码" align="center" prop="materialTrayCode" min-width="150"/>
        <el-table-column label="所在仓库" align="center" prop="warehouseId" min-width="150" :formatter="warehouseFormat"/>
        <el-table-column label="操作人" align="center" prop="userId" min-width="100" :formatter="userNicknameFormat"/>
        <el-table-column label="操作类型" align="center" prop="operateType" min-width="180">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.MATERIAL_OPERATE_TYPE" :value="scope.row.operateType"/>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="createTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-box">
        <!-- 分页组件 -->
        <pagination
          class="noshadowPage"
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          layout="prev, pager, next"
          :background="false"
        />
      </div>
    </el-dialog>
    <!-- 对话框(添加 / 修改) -->
    <MaterialLogForm ref="formRef" @success="getList"/>
    <!-- 对话框(物料详情) -->
    <MaterialDetail ref="detailRef" @success="getList"/>
  </div>
</template>

<script>
import * as MaterialLogApi from '@/api/hs/materiallog';
import MaterialLogForm from './MaterialLogForm.vue';
import {parseTime} from "../../../utils/ruoyi";
import {listSimpleUsers} from "@/api/system/user";
import {DICT_TYPE} from "@/utils/dict";
import {getWarehouseSimpleList} from "@/api/hs/warehouse";
import MaterialDetail from '@/views/hs/componnets/MaterialDetail.vue'

export default {
  name: "MaterialLog",
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  components: {
    MaterialLogForm,
    MaterialDetail
  },
  data() {
    return {
      dialogTitle: "物资溯源",
      dialogVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料日志列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 6,
        userId: null,
        materialId: null,
        materialName: null,
        materialCode: null,
        materialBoxCode: null,
        materialTrayCode: null,
        materialModeCode: null,
        workOrderNo: null,
        operateType: null,
        content: null,
        createTime: [],
      },
      // 用户下拉列表
      users: [],
      warehouseList: [],
    };
  },
  created() {
    // this.getList();
    // 获得用户列表
    listSimpleUsers().then(response => {
      this.users = response.data;
    });
    this.getWarehouseList();
  },
  methods: {
    parseTime,
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        this.dialogVisible = true;
        const res = await MaterialLogApi.getMaterialLogPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 查询仓库列表 */
    async getWarehouseList() {
      getWarehouseSimpleList().then(res => {
        this.warehouseList = res.data;
      })
    },
    // 用户昵称展示
    userNicknameFormat(row, column) {
      if (!row.userId) {
        return '未设置';
      }
      for (const user of this.users) {
        if (parseInt(row.userId) === user.id) {
          return user.nickname;
        }
      }
      return '未知【' + row.operator + '】';
    },
    // 仓库名称展示
    warehouseFormat(row, column) {
      if (!row.warehouseId) {
        return '未设置';
      }
      for (const warehouse of this.warehouseList) {
        if (row.warehouseId === warehouse.id) {
          return warehouse.name;
        }
      }
      return '未知【' + row.warehouseId + '】';
    },
    /** 物料详情 */
    openMaterialDetail(workOrderNo) {
      this.$refs["detailRef"].open(undefined, undefined, workOrderNo);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dialogVisible = false;
      // this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除物料日志编号为"' + id + '"的数据项?')
      try {
        await MaterialLogApi.deleteMaterialLog(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有物料日志数据项?');
      try {
        this.exportLoading = true;
        const data = await MaterialLogApi.exportMaterialLogExcel(this.queryParams);
        this.$download.excel(data, '物料日志.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  }
};
</script>

<style lang="scss" scoped>
  .pagination-box {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 20px;
  }
  ::v-deep .noshadowTable {
    &.el-table {
      box-shadow: none;
    }
  }
  ::v-deep .noshadowPage {
    &.pagination-container {
      box-shadow: none;
    }
  }
  .searchRow {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 20px;
    .el-button {
      padding: 15px 40px;
    }
  }
  ::v-deep .labelRow {
    background: #FFFFFF;
    padding: 20px 30px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .el-input__inner {
      border: none;
    }
  }
  .label-icon {
    display: flex;
    align-items: center;
    gap: 10px;
    .icon-img {
      width: 33px;
      height: 33px;
    }
  }
</style>
